package server

import (
	"shop-api/internal/model"
)

type GoodListInfo struct {
	*model.Good
	Price    string `json:"price"`
	VipPrice string `json:"vip_price"`
}

func GetGoodList(offset, limit, typeID uint, keyword string) ([]*GoodListInfo, error) {
	goodMod := new(model.Good)
	goodInfos, err := goodMod.GetList(offset, limit, typeID, keyword)
	if err != nil {
		return nil, err
	}

	var goodIDs []uint
	for _, val := range goodInfos {
		goodIDs = append(goodIDs, val.ID)
	}

	goodSKUMod := new(model.GoodSKU)
	skuInfos, err := goodSKUMod.GetGoodMinPriceByGoodIDs(goodIDs)
	if err != nil {
		return nil, err
	}

	skuMap := make(map[uint]*model.GoodSKU)
	for _, val := range skuInfos {
		skuMap[val.GoodID] = val
	}

	var res []*GoodListInfo
	for _, val := range goodInfos {
		skuInfo, ok := skuMap[val.ID]
		if !ok {
			continue
		}
		res = append(res, &GoodListInfo{
			Good:     val,
			Price:    skuInfo.Price,
			VipPrice: skuInfo.VipPrice,
		})
	}

	return res, nil
}
